package com.tmeone.erpone.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tmeone.erpone.entity.JsonResult;
import com.tmeone.erpone.entity.SCompany;
import com.tmeone.erpone.entity.SUser;
import com.tmeone.erpone.entity.TCategories;
import com.tmeone.erpone.service.ISCompanyService;
import com.tmeone.erpone.service.ISUserService;
import com.tmeone.erpone.service.ITCategoriesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

@RestController
public class TCategoriesController {

    @Autowired
    private ITCategoriesService categoriesService;

    @Autowired
    private ISUserService userService;

    @Autowired
    private ISCompanyService companyService;

    //查询所有商品类别
    @GetMapping("/categories")
    public JsonResult getAllCategories(){
        List<TCategories> allTCategories = categoriesService.findAllTCategories();
        for (TCategories category : allTCategories) {
            category.setUserId(userService.getById(category.getUserId()).getUserName());
            category.setCompanyId(companyService.getById(category.getCompanyId()).getCompanyName());
            category.setPid(categoriesService.getById(category.getPid()).getName());
            /*for (TCategories categoryCategory : category.getCategories()) {
                categoryCategory.setUserId(userService.getById(categoryCategory.getUserId()).getUserName());
                categoryCategory.setCompanyId(companyService.getById(categoryCategory.getCompanyId()).getCompanyName());
            }*/
        }
        return new JsonResult(1,allTCategories);
    }

    //查询所有商品类别
    @GetMapping("/category")
    public JsonResult getCategory(String id){
        List<TCategories> list = categoriesService.list(new QueryWrapper<TCategories>().lambda().eq(TCategories::getId, id));
        for (TCategories category : list) {
            category.setPid(categoriesService.getById(category.getPid()).getName());
            category.setUserId(userService.getById(category.getUserId()).getUserName());
            category.setCompanyId(companyService.getById(category.getCompanyId()).getCompanyName());
        }
        return new JsonResult(1,list);
    }

    //查询商品的所有父级菜单
    @GetMapping("/categories/pid")
    public JsonResult getAllCategoriesPid(){
        return new JsonResult(1,categoriesService.list(new QueryWrapper<TCategories>()
                .lambda().in(TCategories::getPid, "0","1")));
    }

    //查询商品的分类
    @GetMapping("/category/pid")
    public JsonResult getCategoriesPid(){
        return new JsonResult(1,categoriesService.list(new QueryWrapper<TCategories>()
                .lambda().eq(TCategories::getPid, "1")));
    }

    //添加商品菜单
    @PostMapping("/category")
    public JsonResult addCategory(@RequestBody TCategories category){
        String id = UUID.randomUUID().toString().replace("-","").substring(0,10);
        category.setId(id);
        category.setPid(categoriesService.getOne(new QueryWrapper<TCategories>().lambda().eq(TCategories::getName,category.getPid())).getId());
        category.setUserId(userService.getOne(new QueryWrapper<SUser>().lambda().eq(SUser::getUserName,category.getUserId())).getId());
        category.setCompanyId(companyService.getOne(new QueryWrapper<SCompany>().lambda().eq(SCompany::getCompanyName,category.getCompanyId())).getId());
        categoriesService.save(category);
        return new JsonResult(1,id);
    }

    //修改商品菜单
    @PutMapping("/category")
    public JsonResult modifyCategory(@RequestBody TCategories category){
        category.setPid(categoriesService.getOne(new QueryWrapper<TCategories>().lambda().eq(TCategories::getName,category.getPid())).getId());
        category.setUserId(userService.getOne(new QueryWrapper<SUser>().lambda().eq(SUser::getUserName,category.getUserId())).getId());
        category.setCompanyId(companyService.getOne(new QueryWrapper<SCompany>().lambda().eq(SCompany::getCompanyName,category.getCompanyId())).getId());
        categoriesService.updateById(category);
        return new JsonResult(1,"ok");
    }

    @DeleteMapping("/category/{id}")
    public JsonResult deleteCategory(@PathVariable("id") String id){
        categoriesService.removeById(id);
        return new JsonResult(1,"ok");
    }
}
